# To change this template, choose Tools | Templates
# and open the template in the editor.

class WeightedSum

  attr_accessor :weight, :soil

  def initialize (sim_vector, soil=0.5)
    @weight= sim_vector
    @soil=soil
  end
  def update(sim)
    (0..sim.size-1).each{|i|
      self.weight[i]= (self.weight[i]+sim[i])/2
    }
  end
  # Predice il vettore previsione
  def weighted_sum (matrix_x)
    predizione= []
    av= 0
p self.weight
      self.weight.each{|x|
        if x<=self.soil then
          av= av+0
        else
          av= av+1
        end
      }

    (0..matrix_x[0].size-1).each{ |i|

      average= 0

      (0..matrix_x.size-1).each{|x|
        if self.weight[x]>=self.soil
        average= average + (self.weight[x] * matrix_x[x][i])
        end
      }

      average= average/(av)
      predizione.push(average.round)
      }

    return predizione
  end
  def scored_weight(matrix)

    predizione= []
    score=[]

 
    (0..matrix[0].size-1).each{

      score.push([0,0,0])
    }  

    (0..matrix[0].size-1).each{|x|
    (0..matrix.size-1).each{|i|
        if(matrix[i][x]==1) then
            score[x][0]=score[x][0]+self.weight[i]
        elsif(matrix[i][x]==2)
          score[x][1]=(score[x][1])+self.weight[i]
        elsif(matrix[i][x]==3)
          score[x][2]=score[x][2]+self.weight[i]
        end
      }
    }

    score.each{|s|
      if (s[0]==s.max) then
        predizione.push(1)
        elsif (s[1]==s.max)
          predizione.push(2)
      else
        predizione.push(3)
      end
    }

    return predizione
  end
  def weight
    @weight
  end
end
